package d5.offer11;

/**
 * 二分查找 + 暴力
 */
public class Solution {
    private int[] numbers;

    public int bSearch(int left, int right){
        //middle可能会等于left
        int middle = (left + right);
        while (true) {

            if (left == right) {
                return numbers[left];
            }
            if(right - left == 1){
                return Math.min(numbers[left],numbers[right]);
            }
            boolean reset = false;
            //right需要左移,重设middle
            if (numbers[left] == numbers[right]) {
                reset = true;
            }
            //暴力
            while (numbers[left] == numbers[right]) {
                right -= 1;
                if (left == right)
                    return numbers[left];
            }
            if (reset)
                middle = (left + right) / 2;

            if (numbers[middle] > numbers[right]) {
                left = middle;
            } else {
                right = middle;
            }
            middle = (left + right) / 2;

        }


    }

    public int minArray(int[] numbers){
        // 2 2 2 2 1 2 2 2
        // 如何确定上述数组的最小值
        //二分

        this.numbers = numbers;
        return bSearch(0, numbers.length - 1);
    }

    public static void main(String[] args){
        int[] arr = {1,3,5};
        int min = new Solution().minArray(arr);
        System.out.println(min);

    }
}
